package com.lxx.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxx.wiki.domain.entity.Category;
import com.lxx.wiki.domain.entity.CategoryExample;
import com.lxx.wiki.domain.entity.Ebook;
import com.lxx.wiki.domain.req.CategoryQueryReq;
import com.lxx.wiki.domain.req.CategorySaveReq;
import com.lxx.wiki.domain.resp.CategoryQueryResp;
import com.lxx.wiki.domain.resp.EbookQueryResp;
import com.lxx.wiki.domain.resp.PageResp;
import com.lxx.wiki.mapper.CategoryMapper;
import com.lxx.wiki.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    public List<CategoryQueryResp> all() {
        CategoryExample categoryExample = new CategoryExample();
        //  基于sort进行升序排序
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        List<CategoryQueryResp> categoryQueryRespList = BeanCopyUtils.copyBeanList(categoryList, CategoryQueryResp.class);
        return categoryQueryRespList;
    }

    public void save(CategorySaveReq categorySaveReq) {
        Category category = BeanCopyUtils.copyBean(categorySaveReq, Category.class);
        if (ObjectUtils.isEmpty(category.getId())) {
            // 新增
            // id数据库设置了自增
            categoryMapper.insert(category);
        } else {
            // 修改
            categoryMapper.updateByPrimaryKey(category);
        }

    }

    public void del(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
